#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <sys/wait.h>

#define MAXLINE 1024

int main()
{
  char result_buf[MAXLINE], command[MAXLINE];
  int rc = 0;
  FILE * fp;

  snprintf(command, sizeof(command), "ls ./ | wc -l");

  fp = popen(command, "r");
  if (fp == NULL) {
    perror("popen execute failed.");
    exit(0);
  }

  while (fgets(result_buf, sizeof(result_buf), fp) != NULL) {
    if ('\n' == result_buf[strlen(result_buf) - 1]) {
      result_buf[strlen(result_buf) - 1] = '\0';
    }
    printf("command:%s output:%s\r\n", command, result_buf);
  }

  rc = pclose(fp);
  if (-1 == rc) {
    perror("close file wad fial.");
    exit(1);
  }
  else {
    printf("command:%s child process end status:%d command return value : %s\n", command, rc, WEXITSTATUS(rc));
  }

  return 0;
}
